package com.example.projectjava.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;
@Repository   //此注解的作用：与控制器中的@Controller注解的功能相同，意为将该类纳入Spring容器管理
public class TrolleyDao {
    @Autowired
    JdbcTemplate jdbcTemplate;
    /**
     * 用于往trolley表中插入数据
     *
     */
    public  int trolleyadd(String userName,String goodsName,double price,int count){
        String sql = "insert into trolley(userName,goodsName,price,count) values(?,?,?,?)";
        return    jdbcTemplate.update(sql,userName,goodsName,price,count);//返回了一个整数，用来表示此操作有没有成功
    }
    /**改*/
    public  int trolleyUpdate(String userName,String goodsName,double price,int count){
        String sql = "update trolley set goodsName=?,price=?,count=? where userName=?";
        return    jdbcTemplate.update(sql,goodsName,price,count,userName);//返回了一个整数，用来表示此操作有没有成功
    }
    /**
     * 根据goodsName 删除表中对应的数据
     *
     */
    public  int deleteTrolley(String userName,String goodsName){
        String sql  = "DELETE from trolley where userName=? and goodsName=?";
        return  jdbcTemplate.update(sql,userName,goodsName);
    }
/**查*/
public List<Map<String, Object>> querytrolley(String userName){
    String sql = "select * from trolley where userName=?";
    return  jdbcTemplate.queryForList(sql,userName);}



public List<Map<String,Object>> queryAlltrolley(){
    String sql = "SELECT userName,goodsName,price,count  FROM `trolley` ";


    return  jdbcTemplate.queryForList(sql);
}


}

